package com.ruoyi.system.controller;

import com.ruoyi.system.config.QQEmailConfig;
import com.ruoyi.system.domain.dto.EmailBodyDto;
import com.ruoyi.system.domain.vo.UserInfoByBillInfoIdVo;
import com.ruoyi.system.service.IBillInfoService;
import com.ruoyi.system.utils.QQEmail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;

/**
 * @author xy
 * @version 1.0
 * @description:
 * @date 2024/7/11 19:16
 */
@RestController
@RequestMapping("/system/mail")
@Api(tags = "邮箱模块")
public class EMailController {
    @Autowired
    private QQEmailConfig qqEmailConfig;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private IBillInfoService billInfoService;

    @PostMapping
    @ApiOperation("根据订单ID来发送邮箱")
    public boolean sendEmail(@RequestBody EmailBodyDto dto) {
        Long billInfoId = dto.getBillInfoId();
        UserInfoByBillInfoIdVo userInfoByBillInfoIdVo = billInfoService.selectUserEmailByBillInfoId(billInfoId);
        // 昵称
        String nickName = userInfoByBillInfoIdVo.getNickName();
        // 发送的邮箱地址
        String toMail = userInfoByBillInfoIdVo.getEmail();
        // 账单金额
        float payAmounts = userInfoByBillInfoIdVo.getPayAmounts();
        String body = nickName + "(先生/女士)您好，你上月的电费账单和用电报告已结算，您上月账单共消费"+payAmounts+"元，请快快打钱";
        String subject = "电费账单";
        try {
            QQEmail.sendEmail(javaMailSender,
                    qqEmailConfig.getEmailFrom(),
                    toMail,
                    subject,
                    body
            );
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return false;
    }
}
